from bs4 import BeautifulSoup
import csv

class Parser:
    def __init__(self,filename, filename1):
        self.filename = filename
        self.filename1 = filename1


    def create_doc_from_filename(self,filename):
        print("creating doc...")
        with open(filename, "r", encoding="utf-8") as f:
            html_content = f.read()
            doc = BeautifulSoup(html_content, features="html.parser")
        return doc


    def parse(self,doc):
        print("parsering project-list...")
        post_list = doc.find_all("div", class_="project-list-item")
        with open("OpenEuler_repo.csv", mode="a", encoding="utf-8", newline="") as f:
            writer = csv.writer(f)
            writer.writerow(["title", "href"])
            for post in post_list:
                title = post.find_all("a", class_="repository")
                for t in title:
                    writer.writerow([t.attrs["title"], t.attrs["href"]])


    def parse_repo(self,doc):
        print("parsering repo...")
        with open("OpenEuler_repo_result.csv", mode="a", encoding="utf-8", newline="") as f:
            writer = csv.writer(f)
            writer.writerow(["title", "star-num", "contributor-num"])
            projectlist = doc.find_all("div", class_="site-content")
            for project in projectlist:
                header = project.find_all("div", class_="git-project-header-container")[0]
                title = header.find_all("a", class_="repository")[0]
                star = header.find_all("span", class_="basic buttons star-container ui")[0]
                star_num = star.find_all("a", class_="ui button action-social-count")[0]
                writer.writerow([title.text.strip(), star_num.text.strip()])


    def parse_description(self,doc):
        print("parsering description")
        with open("OpenEuler_description.csv", mode="w", encoding="utf-8", newline="") as f:
            writer = csv.writer(f)
            projectlist = doc.find_all("div", class_="site-content")
            for project in projectlist:
                description = project.find_all("span", class_="git-project-desc-text")[0]
                writer.writerow(description)


    def main(self):
        doc = self.create_doc_from_filename(self.filename)
        self.parse(doc)

    def main1(self):
        doc = self.create_doc_from_filename(self.filename1)
        self.parse_repo(doc)
        self.parse_description(doc)